from django.db import models
from django.contrib.auth.models import User

class Greeting(models.Model):
    author = models.ForeignKey(User, null=True, blank=True)
    content = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    
class Category(models.Model):
    title = models.TextField()
    order = models.IntegerField()
    
    def pages(self):
        return Page.objects.filter(category=self)
    
    def __str__(self):
        return self.title
    
class Page(models.Model):
    category = models.ForeignKey(Category, null=True, blank=True)
    title = models.TextField(verbose_name="Title")
    content = models.TextField()
    order = models.IntegerField(blank=False)
    date = models.DateTimeField(auto_now_add=True)
    
    def slideshows(self):
        listLink = self.slideshowLinks()
        
        listSlideshowsId = list()
        for link in listLink :
            listSlideshowsId.append(link.slideshow.pk)
    
        slideshows = list()
        if( len(listSlideshowsId) > 0):
            slideshows = Slideshow.objects.filter(pk__in=listSlideshowsId).order_by('order')
        return slideshows

    def slideshowLinks(self):
        return LinkSlideshowPage.objects.filter(page=self)
        
    def __str__(self):
        return self.title
    
class Slideshow(models.Model):
    title = models.TextField()
    description = models.TextField()
    order = models.IntegerField()
    
    def photos(self):
        listLink = self.photoLinks()
        
        listPhotoIds = list()
        for link in listLink :
            listPhotoIds.append(link.photo.pk)
    
        photos = list()
        if( len(listPhotoIds) > 0):
            photos = Photo.objects.filter(pk__in=listPhotoIds).order_by('order')
        return photos
        
    def photoLinks(self):
        return LinkPhotoSlideshow.objects.filter(slideshow=self)
        
    def pages(self):
        listLink = self.pageLinks()
        
        listPageIds = list()
        for link in listLink :
            listPageIds.append(link.page.pk)
    
        pages = list()
        if( len(listPageIds) > 0):
            pages = Page.objects.filter(pk__in=listPageIds).order_by('order')
        return pages
        
    def pageLinks(self):
        return LinkSlideshowPage.objects.filter(slideshow=self)
        
    def __str__(self):
        return self.title
    
class Photo(models.Model):
    title = models.TextField()
    description = models.TextField()
    thumb = models.URLField(verbose_name="Thumb (35x35)")
    url = models.URLField(verbose_name="Url (620x378)")
    order = models.IntegerField()
    
    def slideshows(self):
        listLink = self.slideshowLinks()
        
        listSlideshowIds = list()
        for link in listLink :
            listSlideshowIds.append(link.slideshow.pk)
        slideshowsLinked = list()
        if( len(listSlideshowIds) > 0):
            slideshowsLinked = Slideshow.objects.filter(pk__in=listSlideshowIds).order_by('order')
        return slideshowsLinked
    
    def slideshowLinks(self):
        return LinkPhotoSlideshow.objects.filter(photo=self)
    
    def __str__(self):
        return self.title
    
class LinkSlideshowPage(models.Model):
    slideshow = models.ForeignKey(Slideshow, null=False, blank=False)
    page = models.ForeignKey(Page, null=False, blank=False)
    
class LinkPhotoSlideshow(models.Model):
    photo = models.ForeignKey(Photo, null=False, blank=False)
    slideshow = models.ForeignKey(Slideshow, null=False, blank=False)
    